import { GetStaticProps, GetStaticPropsContext } from 'next';
import React, { FC } from 'react';
import { getPosts, getMarkdown, Post, getPostFromDirectory } from '../../utils/Posts';
import Markdown from '../../components/Markdown';
import DefaultPage from '../../templates/Default';

interface Props {
  post: Post;
  markdown: string;
}

const Page : FC<Props> = ({ post, markdown }) => {
    const { lastUpdated } = post.meta;

    return (
      <>
        <DefaultPage
          path={"/logs/" + post.directory}
          lastUpdated={lastUpdated}
        >
        <Markdown
          md={markdown}
        />
        </DefaultPage>
      </>
    );
}

export default Page;

export const getStaticProps: GetStaticProps = async (
    context: GetStaticPropsContext
  ) => {
    const directory = context.params!.directory as string;

    const post = await getPostFromDirectory(directory);

    const markdown = await getMarkdown(post);

    return {
      props: { post, markdown },
    }
}

export async function getStaticPaths() {
  const posts = await getPosts();

  const paths = posts.map((post) => {
    return {
      params: {
        directory: post.directory
      }
    };
  });

  return {
    paths,
    fallback: false
  };
}
